const api = require("../../../ws_micro_shop/utils/api.js");
const app = getApp();

let is_no_more = false;
let is_loading = false;
let p = 2;

Page({
  data: {
    isWithdraw: 0,
    abnorData: {
      imgSrc: "/common/static/images/abnor-04.png",
      text: "暂无销售排行数据"
    },
    begin_time: "",
    end_time: ""
  },
  onLoad() {
    let that = this;
    let showOpenStore =
      app.ws_micro_shop_globalData &&
      (app.ws_micro_shop_globalData.isMulti ||
        app.ws_micro_shop_globalData.isBrand); // 是否为多店或者是品牌版
    that.setData({
      showOpenStore,
      isBrand:
        app.ws_micro_shop_globalData && app.ws_micro_shop_globalData.isBrand //是否为品牌版
    });

    app.loadShopSetting().then(res => {
      that.setData(res);
    });
    app.loadCurrencyUnit().then(res => {
      that.setData({
        unit: res.unit
      });
    });
  },
  onShow() {
    let that = this;
    app.ws_micro_shoprequest({
      url: api.user.index,
      success: function(res1) {
        if (res1.code === 0) {
          let admin = Number(res1.data.user_info.is_admin);
          let isWithdraw = Number(res1.data.user_info.withdraw);
          that.setData({ isWithdraw });
          if (admin === 0) {
            wx.showModal({
              title: "提示",
              content: "你不是管理员，无权访问本页面",
              showCancel: false,
              success: function(res) {
                if (res.confirm) {
                  app.util.navigateTo(
                    "/ws_micro_shop/pages/index/index",
                    {},
                    ""
                  );
                }
              }
            });
          } else {
            that.onGetData();
          }
          //isWithdraw为1获取提现数据
          if (isWithdraw === 1) {
            that.getCashData();
          }
        } else {
          app.util.navigateTo("/ws_micro_shop/pages/index/index", {}, "");
        }
      }
    });
    console.log(that.data);
  },
  openStore(e) {
    // 0是没关 1是关
    let that = this;
    app.ws_micro_shoprequest({
      url: api.adminer.shopupdown,
      data: {
        is_lock: e.detail.value ? 0 : 1
      },
      success: function(res) {
        if (res.code === 0) {
          that.onGetData();
        } else {
          wx.showToast({
            title: res.msg,
            duration: 2000,
            icon: "none"
          });
        }
      }
    });
  },
  onGetData() {
    let that = this;
    wx.showLoading({
      title: "正在加载",
      mask: true
    });
    app.ws_micro_shoprequest({
      url: api.adminer.index,
      success: function(res) {
        if (res.code === 0) {
          that.setData(res.data);
          that.onGetSaleList();
        }
      },
      complete: function() {
        wx.hideLoading();
      }
    });
  },
  onGetSaleList() {
    let that = this;
    p = 2;
    let begin_time = that.data.begin_time;
    let end_time = that.data.end_time;
    that.setData({
      begin: begin_time,
      end: end_time
    });
    if (!((begin_time && end_time) || (!begin_time && !end_time))) {
      wx.showToast({
        title: "请选择日期区间",
        icon: "none"
      });
    }
    app.ws_micro_shoprequest({
      url: api.adminer.goodSaleList,
      data: {
        page: 1,
        begin_time: that.data.begin_time,
        end_time: that.data.end_time
      },
      success: function(res) {
        if (res.code === 0) {
          that.setData({
            order_list: res.data.list
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none"
          });
        }
      },
      complete: function() {
        wx.hideLoading();
      }
    });
  },
  onReachBottom() {
    let _this = this;
    if (is_loading || is_no_more) {
      return;
    }
    is_loading = true;
    app.ws_micro_shoprequest({
      url: api.adminer.goodSaleList,
      data: {
        page: p,
        begin_time: _this.data.begin,
        end_time: _this.data.end
      },
      success: function(res) {
        if (res.code === 0) {
          let order_list = _this.data.order_list.concat(res.data.list);
          _this.setData({
            order_list
          });
          if (res.data.list.length === 0) {
            is_no_more = true;
            // wx.showToast({
            //   title: '没有更多数据了',
            //   icon: 'none',
            // });
          }
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none"
          });
        }
        p++;
      },
      complete: function() {
        is_loading = false;
      }
    });
  },
  //获取提现数据
  getCashData() {
    let that = this;
    app.ws_micro_shoprequest({
      url: api.adminer.withdraw,
      method: "GET",
      success: function(res) {
        console.log("提现");
        console.log(res);
        if (res.code === 0) {
          that.setData({
            accountMoney: Boolean(res.data.account_money)
              ? res.data.account_money
              : "0.00"
          });
          that.setData({
            volume: Boolean(res.data.volume) ? res.data.volume : "0.00"
          });
          that.setData({
            unreceive: Boolean(res.data.unreceive) ? res.data.unreceive : "0.00"
          });
        }
      }
    });
  },
  toListPage(e) {
    let url = "/ws_micro_shop_seller/pages/order_manage/order";
    let dataset = e.currentTarget.dataset;
    app.util.navigateTo(
      url,
      { type: dataset.type, status: dataset.status },
      false
    );
  },
  beginTimeChange: function(e) {
    this.setData({
      begin_time: e.detail.value
    });
  },
  endTimeChange: function(e) {
    this.setData({
      end_time: e.detail.value
    });
  }
});
